<?php
/**
 * Created by IntelliJ IDEA.
 * User: TinND
 * Date: 4/3/14
 * Time: 1:57 PM
 */
header('Content-type: application/json');
$data = json_decode(file_get_contents("php://input"));
$id = $_POST['id'];
$type = $_POST['type'];
if (!empty($_FILES) && isset($id) && isset($type)) {
    $tempPath = $_FILES['file']['tmp_name'];
    list($hostPath) = explode($_SERVER['DOCUMENT_ROOT'], dirname(__FILE__));
    $rootPath = $hostPath . $_SERVER['DOCUMENT_ROOT'] . '/';

    $productPath = $rootPath . 'img/product/' . $id . '/' . $type . '/';
    if (!file_exists($productPath)) {
        mkdir($productPath, 0777, true);
    }

    $uploadPath = $productPath . $ext = "$id." . pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);;

    move_uploaded_file($tempPath, $uploadPath);

    $answer = array('success' => true,
        'message' => 'upload success ' . $uploadPath);
    echo json_encode($answer);
} else {
    echo json_encode(array('success' => false));
}